<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" highlight-current-row @row-click="handleRowClick" @row-contextmenu="rightClick" :height="autoHeight">
      <el-table-column prop="callCmd" label="规则指令" align="center">
        <template slot="header" slot-scope="scope">
          <template v-if="select">
            <div>{{ scope.column.label }}</div>
            <el-input  v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
          <div v-else>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)" style="width: calc(100% - 50px);padding-right: 8px"></el-input>
            <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
          </div>
        </template>
      </el-table-column>
      <template v-if="select">
        <el-table-column prop="ruleName" label="规则名称" align="center">
          <template slot="header" slot-scope="scope">
            <div>{{ scope.column.label }}</div>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="70px">
          <template v-if="!select" slot="header">
            <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
          </template>
          <div slot-scope="scope">
            <el-button v-if="select" type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
            <el-button v-else type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </div>
        </el-table-column>
      </template>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, prev, next"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
    <el-dialog :visible.sync="visible" title="模型信息" width="850px">
      <el-form v-model="entityInfo" label-width="80px" label-position="right">
        <el-form-item label="规则名称" required>
          <el-input v-model="entityInfo.ruleName" placeholder="规则名称"></el-input>
        </el-form-item>
        <el-form-item label="规则指令" required>
          <el-input v-model="entityInfo.callCmd" placeholder="规则指令"></el-input>
        </el-form-item>
        <el-form-item label="规则模板">
          <span style="color: #67c23a">固定参数：YY、YYYY、YYYYMM、YYYYMMDD、YYYYMMDDHH、MM、DD、Q、NO</span>
          <el-input v-model="entityInfo.ruleTemplate" placeholder="以${xxx}为占位符"></el-input>
        </el-form-item>
        <el-row v-if="entityInfo.ruleTemplate" :gutter="8">
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-select style="width: 100%" v-model="entityInfo.keywords" @focus="parseWord" multiple @change="wordChange">
                <el-option v-for="option in keywords" :key="option.value" :value="option.value" :label="option.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流水号">
              <el-input-number v-model="entityInfo.ruleNumber" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="规则计算">
          <idea-code v-model="entityInfo.ruleCompute" :config="{language:'java'}"></idea-code>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveRule(entityInfo)">{{ entityInfo.ruleId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import IdeaCode from "@/components/idea-code/IdeaCode";

export default {
  name: "AppRuleTable",
  components: {IdeaCode},
  mixins: [IdeaTable],
  inject: {
    appContext: {
      from: 'appContext',
      default: function () {
        return {}
      }
    }
  },
  props: {
    select: {
      type: Boolean,
      default: false
    },
    filters: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  computed: {
    autoHeight() {
      return this.select ? `580px` : `calc(100vh - 225px)`
    }
  },
  data () {
    return {
      keywords: []
    }
  },
  created() {
    this.queryUrl = '/ruleInfo/queryPage'
    this.saveUrl = '/ruleInfo/save'
    this.removeUrl = '/ruleInfo/delete/'
    this.rowKey = 'ruleId'
    this.pageInfo.pageSize = 20
    if (this.filters && this.filters.length > 0) {
      this.filters.forEach(item => {
        this.addFilter(item)
      })
    }
    this.addFilter({ field: 'appId', op: '=', value: this.appInfo.appId})
    this.rightButtons.splice(0, this.rightButtons.length)
    this.appendRightBtn({ label: '编辑', icon: 'el-icon-edit', click: this.editData })
    this.appendRightBtn({ label: '删除', icon: 'el-icon-delete', click: this.removeData })
    this.loadSource()
  },
  methods: {
    editData(row){
      this.entityInfo = this.$Util.copy(row)
      this.$set(this.entityInfo, 'keywords', this.$Util.toArray(this.entityInfo.ruleWord))
      this.visible = true
    },
    parseWord() {
      this.keywords.splice(0, this.keywords.length)
      const { ruleTemplate } = this.entityInfo
      if (ruleTemplate) {
        const list = this.$Util.formatProp(ruleTemplate)
        list.forEach(item => {
          if (!['NO'].includes(item)) {
            this.keywords.push({ value: item, label: item })
          }
        })
      }
    },
    wordChange() {
      const { keywords } = this.entityInfo
      if (this.$Util.isArray(keywords)){
        this.$set(this.entityInfo, 'ruleWord', this.$Util.toTextString(keywords))
      } else {
        this.$set(this.entityInfo, 'ruleWord', this.$Util.toTextString([]))
      }
    },
    saveRule(row) {
      this.saveData(row)
    }
  }
}
</script>

<style scoped>

</style>
